package controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import model.CaseRoomInfo;
import model.CaseShelfInfo;
import model.User_list;
import model.V_room_shelf;
import model.V_room_shelf_grid;

import com.et.mvc.JsonView;

public class CaseShelfController extends ApplicationController {
	/**
	 * 返回所属单位全部保管室
	 */
	public JsonView getCaseShelfInfos(int rows, int page) throws Exception {
		User_list ul = (User_list)session.getAttribute("sessionAccount");
		String company_id = ul.getUser_company();
		String cond = "room_company='"+company_id+"'";
		List<Object> tmpArgs = new ArrayList<Object>();
		Object[] args = tmpArgs.toArray();
		String sql = "SELECT * FROM V_ROOM_SHELF S WHERE S.room_company='"+company_id+"'";
		List<V_room_shelf> v_room_shelf=V_room_shelf.findBySql(V_room_shelf.class, sql);
		long total = v_room_shelf.size(); // 查询所属单位全部保管室总数
		List<V_room_shelf> v_room_shelf2 = V_room_shelf.findBySql(V_room_shelf.class, sql, args, "shelf_id", rows, (page - 1) * rows);

		// 构造JSON用的数据结构并返回JSON视图
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", total);
		result.put("rows", v_room_shelf2);
		return new JsonView(result);
	}

	/*
	 * 根据caseRoomId获取一条记录
	 */
	public JsonView getCaseRoomInfo(int id) throws Exception {
		V_room_shelf v_room_shelf = V_room_shelf.find(V_room_shelf.class, id);
		return new JsonView(v_room_shelf);
	}

	/**
	 * 保存新建保险柜
	 */
	public JsonView save(CaseShelfInfo caseShelfInfo) throws Exception {
		User_list ul = (User_list)session.getAttribute("sessionAccount");
		String caseRoomId = caseShelfInfo.getCase_room_id();
		String shelfType = caseShelfInfo.getShelf_type();
		StringBuffer sql= new StringBuffer();
		sql.append("SELECT V.ATTR SHELF_TYPE, SUBSTR(MAX(T.SHELF_NUM), LENGTH(MAX(T.SHELF_NUM)) - 2) + 1 SHELF_NUM\n" );
		sql.append("  FROM V_DICT V\n" );
		sql.append("  LEFT JOIN T_CASE_SHELF_INFO T ON T.SHELF_TYPE = V.C_CODE AND T.CASE_ROOM_ID = '"+caseRoomId+"'\n" );
		sql.append(" WHERE V.P_CODE = '23'\n" );
		sql.append("   AND V.C_CODE = '"+shelfType+"'\n" );
		sql.append(" GROUP BY V.ATTR");

		List<CaseShelfInfo> csi =CaseShelfInfo.findBySql(CaseShelfInfo.class, sql.toString());
		if(!csi.isEmpty()){
			String attr = csi.get(0).getShelf_type();
			String shelf_num = csi.get(0).getShelf_num();
			//如果没有保管柜，那么就用ATTR+001
			if(shelf_num==null || "".equals(shelf_num)){
				shelf_num = attr+"001";
				caseShelfInfo.setShelf_num(shelf_num);
			}else{
				caseShelfInfo.setShelf_num(shelf_num);
			}
		}
		
		caseShelfInfo.save();
		JsonView view = new JsonView("success:true");
		view.setContentType("text/html;charset=utf-8");
		return view;
	}

	/**
	 * 保存修改的单位
	 */
	public JsonView update(int id) throws Exception {
		CaseRoomInfo caseRoomInfo = CaseRoomInfo.find(CaseRoomInfo.class, id);
		updateModel(caseRoomInfo);
		caseRoomInfo.save();
		JsonView view = new JsonView("success:true");
		view.setContentType("text/html;charset=utf-8");
		return view;
	}

	/**
	 * 删除指定的单位
	 */
	public JsonView destroy(int id) throws Exception {
		CaseShelfInfo caseShelfInfo = CaseShelfInfo.find(CaseShelfInfo.class, id);
		caseShelfInfo.destroy();
		JsonView view = new JsonView("success:true");
		view.setContentType("text/html;charset=utf-8");
		return view;
	}
	
	
	/**
	 * 更改状态
	 */
	public JsonView chgStstus(int id) throws Exception {
		CaseShelfInfo caseShelfInfo = CaseShelfInfo.find(CaseShelfInfo.class, id);
		String status = caseShelfInfo.getShelf_status();
		if("1".equals(status)){
			status = "0";
		}else{
			status = "1";
		}
		caseShelfInfo.setShelf_status(status);
		caseShelfInfo.update();
		JsonView view = new JsonView("success:true");
		view.setContentType("text/html;charset=utf-8");
		return view;
	}
	/**
	 * 查看格子使用情况
	 */
	public JsonView 	viewGridUse(int id) throws Exception{
		//根据保管柜编号获取柜子规格和格子使用情况
		String sql = "SELECT * FROM V_room_shelf_grid T WHERE T.SHELF_ID='"+id+"' ORDER BY T.GRID_ID";
		List<V_room_shelf_grid> v_list = V_room_shelf_grid.findBySql(V_room_shelf_grid.class, sql);
		String gridRow="0";
		String gridCol="0";
		String roomName="";
		String shelfName="";
		//如果有记录就把保管柜的行和列查询出来
		if(!v_list.isEmpty()){
			gridRow = v_list.get(0).getShelf_row();
			gridCol = v_list.get(0).getShelf_con();
			roomName = v_list.get(0).getRoom_name();
			shelfName = v_list.get(0).getShelf_name();
		}
		
		// 构造JSON用的数据结构并返回JSON视图
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("gridRow", gridRow);
		result.put("gridCol", gridCol);
		result.put("roomName", roomName);
		result.put("shelfName", shelfName);
		result.put("gridData", v_list);
		
		return new JsonView(result);
	}
	
}
